package com.zhou.controller;

import com.alibaba.fastjson.JSON;
import com.zhou.bean.Express;
import com.zhou.bean.FastMan;
import com.zhou.bean.Message;
import com.zhou.bean.ResultData;
import com.zhou.mvc.ResponseBody;
import com.zhou.service.ExpressService;
import com.zhou.service.FastManService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class CourierController {
    @ResponseBody("/courier/list.do")
    public String selectAll(HttpServletRequest req, HttpServletResponse resp){
        String pageIndex = req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        List<FastMan> fastManList = FastManService.selectAll(Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        int total = FastManService.total();
        ResultData<FastMan> data = new ResultData<>();
        data.setRows(fastManList);
        data.setTotal(total);
        return JSON.toJSONString(data);
    }
    @ResponseBody("/courier/insert.do")
    public String insert (HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String sysPhone = req.getParameter("sysPhone");
        String idCard = req.getParameter("idCard");
        String password = req.getParameter("password");
        FastMan fastMan = new FastMan();
        fastMan.setUsername(username);
        fastMan.setSysPhone(sysPhone);
        fastMan.setIdCard(idCard);
        fastMan.setPassword(password);
        int i = FastManService.addFastMan(fastMan);
        System.out.println(i);
        Message message = new Message();
        if (i!=0){
            message.setStatus(0);
            message.setResult("增加成功");
        }else {
            message.setStatus(-1);
            message.setResult("增加失败");
        }
        System.out.println(message);
        return JSON.toJSONString(message);
    }
    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String sysphone = req.getParameter("sysphone");
        FastMan fastMan = FastManService.findBySysPhone(sysphone);
        //System.out.println(express);
        Message msg = new Message();
        if (fastMan.getSysPhone()==null){
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(fastMan);
        }
        return JSON.toJSONString(msg);
    }
    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String sysPhone = req.getParameter("sysphone");
        int i = FastManService.deleteBySysPhone(sysPhone);
        Message message = new Message();
        if (i!=0){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JSON.toJSONString(message);
    }
    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String sysPhone = req.getParameter("sysPhone");
        String idCard = req.getParameter("idCard");
        String password = req.getParameter("password");
        FastMan fastMan = new FastMan();
        fastMan.setId(Integer.parseInt(id));
        fastMan.setUsername(username);
        fastMan.setSysPhone(sysPhone);
        fastMan.setIdCard(idCard);
        fastMan.setPassword(password);
        int i = FastManService.updateFastMan(Integer.parseInt(id), fastMan);
        Message message = new Message();
        if (i != 0){
            message.setStatus(0);
            message.setResult("修改成功");
        }else {
            message.setStatus(-1);
            message.setResult("修改失败");
        }
        return JSON.toJSONString(message);
    }
}
